package com.shugl.demo.utils;

import com.google.common.collect.Lists;
import com.shugl.demo.exception.ParamException;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.util.List;

/**
 * Description
 *
 * @author ShuGuili
 * @version 1.0
 * @since 2020/9/28 14:59
 */
public class NotNullCheck {

    public static void check(Object object) {
        Class cls = object.getClass();
        List<String> fieldNames = Lists.newArrayList();
        for (Field field : cls.getDeclaredFields()) {
            if (field.isAnnotationPresent(NotNull.class)) {
                try {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        fieldNames.add(field.getName());
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        if (!fieldNames.isEmpty()) {
            throw new ParamException(fieldNames.toString() + "不能为空");
        }
    }
}
